package org.itboys.mobile.service.mysql.web.score;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.entity.mysql.score.SundayScoreConfig;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 日期：2017年5月10日
 * 描述：圣代_积分规则_service_web
 */
@Service
public class SundayScoreConfigService extends BaseMySqlService<SundayScoreConfig>{
    /**
     * 新增积分配置  如果有则修改
     * @param config
     */
    public void saveConfig(SundayScoreConfig config){
        if(config.getId()==0l){
            //有且只能有一个
            SundayScoreConfig old = findOne(null);
            if(old!=null){
                throw new ErrorException("请勿重复添加积分规则。");
            }
            super.insert(config);
        }else{
            super.update(config);

        }
    }

    /**
     * 查找积分配置
     * @param configId
     * @return
     */
    public SundayScoreConfig findOne(Long configId){
        if(!LongUtil.isNotZreo(configId)){
            Map<String,Object> param = new HashMap<String, Object>();
            List<SundayScoreConfig> configs = super.select(param);
            if(ListUtil.isNotNull(configs))return configs.get(0);
            return null;
        }else{
            return super.find(configId);
        }
    }

    /**
     * 查找积分配置
     * @param request
     * @return
     */
    public PageResult<SundayScoreConfig> selectConfig(HttpServletRequest request){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        return new PageResult<SundayScoreConfig>(super.select(param),super.count(param));

    }
}
